home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Compendium Deluxe 2
/
LSD and 17bit Compendium Deluxe - Volume II.iso
/
a
/
prog
/
asmsrc
/
thesource-7.lha
/
Source
/
DefFunc.lha
/
DefFunc
/
defunc.3
next >
Wrap
Text File
|
1993-12-14
|
5KB
|
199 lines
DEFUNC(3) C LIBRARY FUNCTIONS DEFUNC(3)
NAME
_d_e_f_u_n_c -- a portable C library package for runtime function
constructing.
DESCRIPTION
_d_e_f_u_n_c (Dynamic Expressible Function Constructing) is a
portable C library package for constructing functions from
runtime inputted expressions.
_d_e_f_u_n_c library function _d_f_o_p_e_n() accepts an expression
string and, on success, returns a function pointer. e.g.
#include <defunc.h>
foo()
{
double (*fnctptr)();
....
fnctptr = dfopen("(x*x+y*y)^0.5");
....
};
The function dfopen() is reenterable. That is, you can
repeatedly call dfopen() without worry about the new result
may overlap or damage the old one, as long as you assign
them to different lvalues. e.g.
double (*fnctptr1)();
double (*fnctptr2)();
...
fnctptr1 = dfopen("exp(-x*x)");
fnctptr2 = dfopen("exp(-x)*sin(x)");
...
Then, fnctptr1 and fnctptr2 will point to two different
functions.
Function dfopen() parses the expression be passed based on
tokens it recognized. Except for numerical string constant
tokens(i.e. anonymous constant tokens) and 8 build in tokens
"+", "-", "*", "/", "^", "(", ")", "," plus a pseudo token
"=", all other tokens are external tokens. External tokens
include argument tokens, function tokens and named constant
tokens. They are stored in a global token table. They can be
set/reset or Added/deleted statically as well as dynami-
cally. e.g.
nameargu("re", "im");
This will reset argument tokens to "re" and "im"(the default
are "x" and "y").
defunc 1.2 Last change: Dec. 1993 1
DEFUNC(3) C LIBRARY FUNCTIONS DEFUNC(3)
namefnct("log", log);
namefnct("ln" , log);
Here, the external function log() has been added to the
token list with 2 alias names "log" and "ln".
namecnst("pi", 2.0*asin(1.0));
namecnst("PI", 2.0*asin(1.0));
This will add a named constant token to the list with alias
names "pi" and "PI" and value 3.1415926... .
clrcnst("PI");
This will delete the constant token "PI" from the global
token table. After an external token has been put into the
global token list, it can be used in expressions passed to
_d_f_o_p_e_n(). e.g. After you put the exponential function exp()
into token table with
namefnct("exp", exp);
/* add exp() to token table statically */
Than you can use it to construct a gaussian function dynami-
cally as
double (*gauss)();
...
gauss = dfopen("exp(-x*x)");
Argument tokens, named constant tokens and dynamically con-
structed functions can be reset or added to the token list
dynamically. This can be done directly from the expression
passed to _d_f_o_p_e_n() function. e.g.
fnctptr = dfopen("Rho(re, im)=(re*re+im*im)^0.5");
This will reset the argument tokens to "re" and "im", return
a duplex function pointer and add this function pointer to
the global token list with name "Rho".
dfopen("gauss(x)=exp(-x*x)");
This sets the first argument tokens to "x" and 2nd one to
none and put the return function pointer to the global token
table with name "gauss" while the return function pointer is
not assigned to any lvalue. Now, the token "gauss" is recog-
nizable by _d_e_f_u_n_c and you can in turn use it to construct
new function. Such as
dfopen("gauss2d(x,y)=gauss(x)*gauss(y)");
defunc 1.2 Last change: Dec. 1993 2
DEFUNC(3) C LIBRARY FUNCTIONS DEFUNC(3)
will return a 2 dimension gaussian function and put it to
token table with name "gauss2d". If you don't want this 2
dimension gaussian function be putted into the token table
but you still want reset the argument tokens, for example to
"a", "b", you can use
fnctptr = dfopen("(a, b) = gauss(a)*gauss(b)");
or
fnctprt = dfopen("gauss(a, b) = gauss(a)*gauss(b)");
Here, fnctptr is a function pointer to accept the returned
value from _d_f_o_p_e_n().
Named constant tokens can also be added to the token list
from expression passed to _d_f_o_p_e_n(). e.g.
fnctptr = dfopen("pi = 4*atan2(1,1)");
This will return a constant function and add a named con-
stant token to the token list with value 3.1415926... and
name "pi".
SEE ALSO
dfopen(3)
AUTHOR
Ke Jin
Physics Department
Queen's University
Kingston, Ontario
Canada K7L 3N6
jinke@sparky.phy.queensu.ca
BUGS
Report bugs of _d_e_f_u_n_c library to the author by email.
defunc 1.2 Last change: Dec. 1993 3